package com.creditease.mobile.rss.ui;

import java.util.Random;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.Display;
import android.view.KeyEvent;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.TextView;

import com.creditease.mobile.R;
import com.creditease.mobile.news.ui.MainTabActivity;
import com.creditease.mobile.rss.RssConst;
import com.creditease.mobile.util.FileUtil;

public class LoadingActity extends Activity implements AnimationListener {
    private final static String TAG = LoadingActity.class.getName();
    private ImageView imageView;
    private TextView textView;
    private Animation alphaAnimation = null;
    private String xmlString;
    private String url;
    public static int screenWidth;
    public static int screenHeight;

    @Override
    public void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	getWindow().requestFeature(Window.FEATURE_PROGRESS);
	/*
	 * //设置无标题 requestWindowFeature(Window.FEATURE_NO_TITLE); //设置全屏
	 * getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
	 * WindowManager.LayoutParams.FLAG_FULLSCREEN);
	 */

	SharedPreferences sharedPreferences = getSharedPreferences(
		RssConst.APP_NAME, Context.MODE_PRIVATE);
	int width = sharedPreferences.getInt("screenWidth", 0);
	if (width == 0) {
	    WindowManager windowManager = getWindowManager();
	    Display display = windowManager.getDefaultDisplay();
	    screenWidth = display.getWidth();
	    screenHeight = display.getHeight();
	    Editor editor = sharedPreferences.edit();// 获取编辑器
	    editor.putInt("screenWidth", screenWidth);
	    editor.putInt("screenHeight", screenHeight);
	    editor.commit();// 提交修改

	}
	setContentView(R.layout.loading);
	imageView = (ImageView) findViewById(R.id.imageView);

	int random = new Random().nextInt(4);
	Bitmap bitmap = FileUtil.getLoacalBitmap(getApplicationContext(),
		"images/loading" + random + ".jpg");
	imageView.setImageBitmap(bitmap);
	textView = (TextView) findViewById(R.id.loadingText);
	textView.setText("遇见你，遇见你是我这辈子最最温暖的事了。");

	alphaAnimation = AnimationUtils.loadAnimation(this,
		R.anim.loading_alpha);
	alphaAnimation.setFillEnabled(true); // 启动Fill保持
	alphaAnimation.setFillAfter(true); // 设置动画的最后一帧是保持在View上面
	imageView.setAnimation(alphaAnimation);
	alphaAnimation.setAnimationListener(this); // 为动画设置监听

    }

    @Override
    public void onAnimationEnd(Animation animation) {
	// 动画结束时结束欢迎界面并转到软件的主界面
	Intent intent = new Intent(this, MainTabActivity.class);
	// intent.putExtra("xml", xmlString);
	// intent.putExtra("url", url);
	startActivity(intent);
	this.finish();
    }

    @Override
    public void onAnimationRepeat(Animation animation) {

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
	// 在欢迎界面屏蔽BACK键
	if (keyCode == KeyEvent.KEYCODE_BACK) {
	    return false;
	}
	return false;
    }

    @Override
    public void onAnimationStart(Animation animation) {
    }
}
